package cn.tedu.reflect;

import cn.tedu.annotation.Value;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class FieldTests {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        //1.通过反射构建Point类型对象
        Class<Point> pointClass = Point.class;
        Constructor<Point> declaredConstructor = pointClass.getDeclaredConstructor();
        Point point = declaredConstructor.newInstance();
        //2.通过反射为Point类中的属性赋值(把Value注解中value的值赋值给它描述的属性)
        Field x = pointClass.getDeclaredField("x");
        Value v=x.getAnnotation(Value.class);
        //设置私有属性可访问
        x.setAccessible(true);
        //将注解Value对象中value方法的返回值赋值给point对象的x属性，
        x.set(point,v.value());
        System.out.println(point);
    }
}
